package com.ruix.app.mapper;

import com.ruix.app.model.ClassMenber;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by hadexs on 2017/4/26.
 */

public class ClassMenberMapper implements ApiMapper<ArrayList<ClassMenber>> {

    @Override
    public ArrayList<ClassMenber> transform(JSONObject response) {
        ArrayList<ClassMenber> list = null;
        if (response != null) {
            JSONObject data = response.optJSONObject("data");
            if (data != null) {
                JSONArray array = data.optJSONArray("list");
                if (array != null && array.length() > 0) {
                    list = new ArrayList();
                    for (int i = 0; i < array.length(); i++) {
                        ClassMenber classMenber = new ClassMenber().parse(array.optJSONObject(i));
                        list.add(classMenber);
                    }
                    //
                    //服务端排序
//                    Collections.sort(list,new Comparator<ClassMenber>() {
//
//                        @Override
//                        public int compare(ClassMenber o1, ClassMenber o2) {
//                           int onew =   Integer.parseInt(o1.getRole());
//                           int two =  Integer.parseInt(o2.getRole());
//                            if (two > onew){
//                                return -1;
//                            } else if (onew == two){
//                                return  0;
//                            } else {
//                                return 1;
//                            }
//                        }
//                    });
                }
            }
        }

        return list;
    }
}
